//	ID : pallab81
// 	PROG : 10616
// 	LANG : C++
// 	2011-09-01-12.01.19 Thursday
//
// 	"I have not failed, I have just found 10000 ways that won't work.
//


#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>

#include <ctime>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>
#include <cstdlib>

using namespace std;

#define VI vector< int >
#define CI( x ) scanf("%d",&x)
#define CL( x ) scanf("%lld",&x)
#define CD( x ) scanf("%lf",&x )
#define fo(i,st,ed) for(int i = st ; i < ed ; ++i )
#define foE(i,st,ed) for(int i = st ; i <= ed ; ++i )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip system("pause")
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define f first
#define s second
#define SET( ARRAY , VALUE ) memset( ARRAY , VALUE , sizeof(ARRAY) )

inline void wait( double seconds ) {
    double endtime = clock()+( seconds* CLOCKS_PER_SEC );
    while( clock() < endtime ) {
        ;
    }
}
int N,Q,D;
int cases;
VI w;
Int dp[202][12][22];

inline int ModifiedMod(int number, int modulus) {
    int result = number % modulus;
    if (result < 0) result += modulus;
    return result;
}

inline void Read() {
    w.assign(N+2,0);
    foE(i,1,N) {
        CI(w[i]);
    }
}
//id = current element in the array
//s  = current sum
//t = currently taken elements
inline Int go(int id, int t, int s) {
    if(t==0 ) {
        return (s==0 ? 1 : 0 ) ;
    }
    if(id==0 ) {
        return 0;
    }
    Int& total= dp[id][t][s];
    if(total!=-1) {
        return total;
    }
    total=0;
    //current item ta niye
    total+= go(id-1, t-1,  ModifiedMod(s+w[id],D ) );
    //current item ta na niye
    total+= go(id-1,t, s);

    return total;
}
inline void Proc() {
    int M;
    printf("SET %d:\n",cases);
    foE(i,1,Q) {
        SET(dp,-1);
        CI(D);
        CI(M);
        Int var = go(N,M,0);
        printf("QUERY %d: %lld\n",i,var);
    }
}
int main() {
//    #ifndef ONLINE_JUDGE
//        freopen("in.txt","rt",stdin);
//    #endif

    for(cases=1; scanf("%d %d",&N,&Q)==2 ; ++cases) {
        if( N==0 && Q==0 )break;
        Read();
        Proc();
    }


    return 0;
}
